package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.WaterMeter;
import com.ruoyi.system.mapper.WaterMeterMapper;
import com.ruoyi.system.service.IWaterMeterService;
import com.ruoyi.system.service.IWaterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 表具信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-16
 */
@Service
public class WaterMeterServiceImpl implements IWaterMeterService {
    @Autowired
    private WaterMeterMapper waterMeterMapper;

    @Autowired
    private IWaterUserService waterUserService;

    /**
     * 查询表具信息
     *
     * @param id 表具信息主键
     * @return 表具信息
     */
    @Override
    public WaterMeter selectWaterMeterById(Long id) {
        return waterMeterMapper.selectWaterMeterById(id);
    }

    @Override
    public WaterMeter selectWaterMeterByMeterNo(String meterNo) {
        return waterMeterMapper.selectWaterMeterByMeterNo(meterNo);
    }

    /**
     * 查询表具信息列表
     *
     * @param waterMeter 表具信息
     * @return 表具信息
     */
    @Override
    public List<WaterMeter> selectWaterMeterList(WaterMeter waterMeter) {
        return waterMeterMapper.selectWaterMeterList(waterMeter);
    }

    /**
     * 新增表具信息
     *
     * @param waterMeter 表具信息
     * @return 结果
     */
    @Override
    public int insertWaterMeter(WaterMeter waterMeter) {
        waterMeter.setCreateTime(DateUtils.getNowDate());
        String userId = waterMeter.getUserId();
        if (userId != null) {
            String priceType = waterUserService.selectWaterUserByUserId(userId).getPriceType();
            if (priceType.equals("1")) {
                waterMeter.setPriceCode("202509221157");
            } else if (priceType.equals("2")) {
                waterMeter.setPriceCode("202509221756");
            } else if (priceType.equals("3")) {
                waterMeter.setPriceCode("202509221758");
            } else {
                waterMeter.setPriceCode("202509221760");
            }
        }
        waterMeter.setInitialReading("0");
        waterMeter.setCurrentReading("0");
        waterMeter.setControlStatus("0");
        return waterMeterMapper.insertWaterMeter(waterMeter);
    }

    /**
     * 修改表具信息
     *
     * @param waterMeter 表具信息
     * @return 结果
     */
    @Override
    public int updateWaterMeter(WaterMeter waterMeter) {
        waterMeter.setUpdateTime(DateUtils.getNowDate());
        return waterMeterMapper.updateWaterMeter(waterMeter);
    }

    @Override
    public int updateWaterMeterNo(WaterMeter waterMeter) {
        waterMeter.setUpdateTime(DateUtils.getNowDate());
        return waterMeterMapper.updateWaterMeterNo(waterMeter);
    }

    /**
     * 批量删除表具信息
     *
     * @param ids 需要删除的表具信息主键
     * @return 结果
     */
    @Override
    public int deleteWaterMeterByIds(Long[] ids) {
        return waterMeterMapper.deleteWaterMeterByIds(ids);
    }

    /**
     * 删除表具信息信息
     *
     * @param id 表具信息主键
     * @return 结果
     */
    @Override
    public int deleteWaterMeterById(Long id) {
        return waterMeterMapper.deleteWaterMeterById(id);
    }


    @Override
    public String getMaxMeterNo() {
        String maxMeterNo = waterMeterMapper.getMaxMeterNo();
        Date time = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateTime = sdf.format(time);
        if (StringUtils.isEmpty(maxMeterNo)) {
            maxMeterNo = dateTime + "002" + "001";
        } else {
            Long ins = Long.valueOf(maxMeterNo);
            ins = ins + 1;
            maxMeterNo = ins.toString();
        }
        return maxMeterNo;
    }
}
